<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
	<link rel="stylesheet" type="text/css" href="../../css/api.css" />
	<link rel="stylesheet" type="text/css" href="../../css/common.css" />
	<link rel="stylesheet" type="text/css" href="../../css/aui.css" />
  <title>发布</title>
  <style>
		html, body {width: 100%;min-height: 100%;background: #fff;}
    .h20 {height: 0.5rem;background: #f0f0f0;}

    .fbxq {
      padding: 0 1rem;
      font-size: 0.7rem;
      color: #999;
    }
    .fbxq > div {
      height: 2.2rem;
      line-height: 2.2rem;
    }
    .fbxq input[type="datetime-local"],
    .fbxq input[type="text"] {
      width: 60%;
    font-size: 0.7rem;
    color: #999;
      height: 2.2rem;
      line-height: 2.2rem;
      border: none;
      display: inline-block;
      vertical-align: middle;
    }
    .fbxq h6 {
      color: #999;
      font-size: 0.6rem;
      line-height: 0.8rem;
      margin-top: 0.5rem;
    }
    .fbxq  > div.upload {
      height: auto;
      margin-top: 0.5rem;
      position: relative;
      background: #ececec;
      width: 8rem;
      padding: 2rem 0;
      box-sizing: content-box;
    }
    .fbxq .upload img {
      width: 32px;
      margin: auto;
    }
    .fbxq input[type="file"] {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      opacity: 0;
      z-index: 1;
    }

    .fbxq textarea {
      font-size: 0.7rem;
      color: #999;
    }
    input[placeholder]{
      font-size: 0.7rem
    }

        .z_file {

            /*background: url(../../image/z_add.png) no-repeat;
            background-size: 100% 100%;*/
            width: 4rem;
            height: 4rem;
            margin-right: 0.2rem;
            margin: 10px;
            position: relative;
        }
        .z_file > div {
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
          z-index: 5;
        }
        .z_file > span {
          position: absolute;
          top: 3px;
          right: 3px;
          font-size: 0.8rem;
          line-height: 1rem;
          text-align: center;
          width: 1rem;
          height: 1rem;
          z-index: 11;
          background: #ff5757;
          border-radius: 50%;
          -webkit-border-radius: 50%;
        }
        .z_file #addImg{
          width: 4rem;
        }

        .z_file input::-webkit-file-upload-button {



        }

        .z_file input#file {
            display: block;
            width: auto;
            border: 0;
            vertical-align: middle;
        }

.imglist{
  width: 100%;
  position: relative;
}
.aui-list-item-input{
  margin-left: 10px
}
    #header_top {
      background-color: #2997e4;
      padding-top: 25px;
      position: fixed;
      width: 100%;
    }
    body {
      padding-top: 65px;
    }
	</style>


</head>
<body>
<div id="app">
    <header id="header_top" class="aui-bar aui-bar-nav" >

      <a class="aui-pull-left"  onclick="goback()">
          <span class="aui-iconfont aui-icon-left"></span>
      </a>
      <div class="aui-title" id="title">
      发布活动
    </div>
        <a class="aui-pull-right" @click="fb()">
            发布
        </a>
    </header>


    <div class="aui-content aui-margin-b-15">
        <ul class="aui-list aui-form-list">

            <li class="aui-list-item" style="height:1px;min-height: 1px;"></li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        活动标题：
                    </div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="" v-model="title">
                    </div>
                </div>
            </li>

            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        活动开始时间：
                    </div>
                    <div class="aui-list-item-input">
                        <!-- <input type="datetime-local" placeholder="" v-model="apply_date" required min="2016-06-27T15:43:08+08:00" value="2016-01-11T16:00:00"> -->
                        <!-- <input id="apply_date" v-model="apply_date" type="text" name="apply_date" placeholder="" @click="setDateSelect('apply_date')"   /> -->
                        <div id="apply_date" @click="setDateSelect('apply_date')" v-model="apply_date" style="width:100%;     min-height: 2.2rem; line-height:50px"></div>
                    </div>
                </div>
            </li>

            <li class="aui-list-item">
              <div class="aui-list-item-inner">
                  <div class="aui-list-item-label">
                      活动结束时间：
                  </div>
                  <div class="aui-list-item-input">
                      <!-- <input id="activity_end_date" type="text" readonly="" v-model="activity_end_date" @click="setDateSelect('activity_end_date')"> -->
                      <div id="activity_end_date" @click="setDateSelect('activity_end_date')" v-model="activity_end_date" style="width:100%;     min-height: 2.2rem; line-height:50px"></div>
                  </div>
              </div>
          </li>

          <li class="aui-list-item">
            <div class="aui-list-item-inner">
                <div class="aui-list-item-label">
                    报名结束时间：
                </div>
                <div class="aui-list-item-input">
                    <!-- <input id="end_date"  type="text" readonly="" v-model="end_date" @click="setDateSelect('activity_end_date')"> -->
                    <div id="end_date" @click="setDateSelect('end_date')" v-model="end_date" style="width:100%;     min-height: 2.2rem; line-height:50px"></div>
                </div>
            </div>
        </li>
        <li class="aui-list-item">
          <div class="aui-list-item-inner">
              <div class="aui-list-item-label">
                  活动地点：
              </div>
              <div class="aui-list-item-input">
                  <input type="text" placeholder="" v-model="address">
              </div>
          </div>
      </li>
      <li class="aui-list-item">
        <div class="aui-list-item-inner">
            <div class="aui-list-item-label">
                活动费用：
            </div>
            <div class="aui-list-item-input">
                <input type="text" placeholder="" v-model="apply_cost">
            </div>
        </div>
    </li>
    <li class="aui-list-item" >
      <div class="aui-list-item-inner">
          <div class="aui-list-item-label">
              添加图片：
          </div>
          <div class="aui-list-item-input">
            <div class="z_file" >
              <img src="../../image/z_add.png" id="addImg" @click="showAction()" />
              <div :style="'background:url('+imageStr+') no-repeat center/cover'" v-show="imageStr != ''"></div>
              <span @click="clearImg" v-show="imageStr != ''">×</span>
            </div>
            </div>
      </div>
  </li>

  <li class="aui-list-item">
    <div class="aui-list-item-inner">
        <div class="aui-list-item-label">
            活动详情：
        </div>
        <div class="aui-list-item-input">
            <textarea id="details" placeholder="请在此输入活动详情" v-model="activity_details"></textarea>
        </div>
    </div>
</li>


        </ul>
  </div>
</div>
</body>

<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/zepto.js"></script>
<script type="text/javascript" src="../../script/vue.min.js"></script>
<script type="text/javascript" src="../../script/utils.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>


<script type="text/javascript">
function goback () {
  var jsfun = 'window.location.reload()';

  api.execScript({
    name: 'index_sj_hd',
    frameName: 'index_sj_hd',
    script: jsfun
});
  api.closeWin({name:api.pageParam.name});
}
// 完成首页初始化
apiready = function(){
//  var FNImageClip = api.require('FNImageClip');



	var app = new Vue({
		el: '#app',
		data: {
      title: '',
      apply_date: '',
      end_date: '',
      activity_end_date: '',
      activity_details: '',
      apply_cost: '',
      address: '',
      imageStr: '',
      interestid: '',
      token:''
		},
		created: function() {


      var $_this = this;

      $_this.token = $api.getStorage('token')

      if(!$api.getStorage('token')){
            			api.confirm( {
            					title: '是否登录',
            					msg: '确认是否登录？',
            					buttons: [ '确定', '取消' ]
            				}, function ( ret, err ) {
            					var index = ret.buttonIndex;
            					if ( index == 1 ) {
            						api.openWin({
            							name: 'login',
            							url: '../five_frame/login_header.html',
            							delay: 200,
            							bounces: false
            						});
            						return;
            					} else {
            						api.closeWin({
            								name: name
            						});
            						return
            					}
            				})
            				return
            			}

		},

		methods: {

      setDateSelect:function(el){
        var $_this = this;
        var myDate = new Date();//获取系统当前时间
        myDate.toLocaleString()
        api.openPicker({
            type: 'date_time',
            date: myDate.getFullYear()+' 12:30',
            title: '选择日期'
        }, function(ret, err) {
            if (ret) {
              var year = ret.year;
              var month = ret.month;
              var day = ret.day;
              //这是选择的是日期
              var value1 = year+'-'+month+'-'+day;
              //选择时间
                api.openPicker({
                    type: 'time',
                    title:'选择时间'
                },function(ret,err){
                    var hour = ret.hour < 10 ? '0' + ret.hour : ret.hour;
                    var minute = ret.minute < 10 ? '0' + ret.minute : ret.minute;
                    //选择的时间
                    var value2 = hour+':'+minute;

                    //组装一下
                    var sdate = ''+value1+' '+value2+''+':00';
                    //$_this.el = sdate;
                    $("#"+el).html(sdate)
                    $_this[el] = sdate

                });
            } else {
                alert(JSON.stringify(err));
            }
        });
      },
      fb: function () {
        var $_this = this;

        if ($_this.title == '') {
          //alert('请输入活动标题')
          api.toast({ msg: '请输入活动标题',duration: 2000,location:'middle' });
          return
        } else if ($_this.apply_date == '') {
          //alert('请输入活动开始时间')
          api.toast({ msg: '请输入活动开始时间',duration: 2000,location:'middle' });
          return
        } else if ($_this.activity_end_date == '') {
          //alert('请输入活动结束时间')
          api.toast({ msg: '请输入活动结束时间',duration: 2000,location:'middle' });
          return
        } else if ($_this.end_date == '') {
          //alert('请输入报名结束时间')
          api.toast({ msg: '请输入报名结束时间',duration: 2000,location:'middle' });
          return
        } else if ($_this.address == '') {
          //alert('请输入活动地址')
          api.toast({ msg: '请输入活动地址',duration: 2000,location:'middle' });
          return
        } else if ($_this.activity_details == '') {
          //alert('请输入活动详情')
          api.toast({ msg: '请输入活动详情',duration: 2000,location:'middle' });
          return
        }
        if($_this.imageStr == ''){
          api.toast({
              msg: '请上传图片',
              duration: 2000,
              location:'middle'
          });
          return;
        }
  			api.ajax({
  				url: window.Url.addActivity,
          method: 'post',
          headers: {
			        'Content-Type': 'application/json',
                  'baseParams': $api.getStorage('token') //token
          },
          data: {
            body: {
              title: $_this.title, //活动标题
              apply_date: $_this.apply_date, //活动开始时间
              end_date: $_this.end_date, //报名结束时间
              activity_end_date: $_this.activity_end_date, //活动结束时间
              activity_details: $_this.activity_details, //活动详情
              apply_cost: $_this.apply_cost, //活动花费
              address: $_this.address, //活动地址
              image: $_this.imageStr,
              interestid: api.pageParam.id, //兴趣圈id
            }
          },
  				timeout: 300,
  			}, function(ret, err) {
  					if (ret) {
              if (ret.code == 2) {
                  alert('用户未登录，请返回登录')
                  $api.rmStorage('token')
                  $api.rmStorage('data')
                  goback()
                   return
                }
              // api.alert({ msg: JSON.stringify(ret) });
              if (ret.code == 0) {
                api.toast({ msg: '发布成功',duration: 2000,location:'middle' });
                goback()
              } else {
                api.alert({ msg: JSON.stringify(ret.message) });
              }
  					} else {
  							api.alert({ msg: JSON.stringify(err) });
  					}
  			})
      },
      // 上传图片
      showAction: function (){
        var $_this = this
        api.actionSheet({
            title: '活动图片上传',
            cancelTitle: '取消',
            buttons: ['拍照','从手机相册选择']
        }, function(ret, err) {
            if (ret) {
                $_this.getPicture(ret.buttonIndex);
            }
        });
      },
      clearImg: function (e) {
        var $_this = this
        $_this.imageStr = ''
        e.stopPropagation()
      },
      getPicture: function (sourceType) {
        var $_this = this
              if(sourceType==1){ // 拍照
                  api.getPicture({
                      sourceType: 'camera',
                      encodingType: 'jpg',
                      mediaValue: 'pic',
                      allowEdit: false,
                      destinationType: 'base64',
                      quality: 90,
                      saveToPhotoAlbum: true
                  }, function(ret, err) {
                      if (ret) {

                        var formData=ret.data;
                        var formData=ret.base64Data;
                        api.ajax({
                            method:"post",
                            url:window.Url.addPicture,
                               data: {
                                 files: {file: ret.data}
                               }

                        },function(ret,err){
                            if(ret){
                              if(ret.error == '0')
                              {
                                  $_this.imageStr = ret.url
                              }
                              else{
                                api.alert({msg:ret.message});
                              }
                              console.log("图片返回"+JSON.stringify(ret))
                            }else{
                                api.toast({
                                  msg : '你没有选择图片',
                                  location : 'middle'
                                })
                            }
                        })
                      }else {
                          alert(JSON.stringify(err));
                      }
                  });
              }
              else if(sourceType==2){ // 从相机中选择
                  api.getPicture({
                          sourceType: 'album',
                          encodingType: 'png',
                          mediaValue: 'pic',
                          destinationType: 'url',
                          allowEdit:true,
                          quality: 50,
                          saveToPhotoAlbum:false,
                          targetWidth: 750,
                          targetHeight: 750
                      }, function(ret, err) {
                          if (ret) {
                              var formData=ret.data;
                              // $('.img').prop('src',ret.data);
                              var formData=ret.base64Data;
                              api.ajax({
                                  method:"post",
                                  url:window.Url.addPicture,
                                     data: {
                                       files: {file: ret.data}
                                     }

                              },function(ret,err){
                                  if(ret){
                                    if(ret.error == '0')
                                    {
                                        $_this.imageStr = ret.url
                                    }
                                    else{
                                      api.alert({msg:ret.message});
                                    }
                                    console.log("图片返回"+JSON.stringify(ret))
                                  }else{
                                      api.toast({
                            						msg : '你没有选择图片',
                            						location : 'middle'
                            					})
                                  }
                              })
                          } else {
                              api.alert({msg:JSON.stringify(err)});
                          }
                  });
              }
          }
		}
	})
}
</script>
</html>
